//
//  LTChooseBaseViewController.m
//  LearnTeacher
//
//  Created by 高刘通 on 2024/6/7.
//

#import "LTChooseBaseViewController.h"

@interface LTChooseBaseViewController ()<UIGestureRecognizerDelegate>
@property(strong, nonatomic) UIView *backgroundView;
@property(strong, nonatomic) UIView *topView;
@property(strong, nonatomic) UIView *contentView;
@property(strong, nonatomic) UILabel *titleLabel;
@property(strong, nonatomic) UIButton *closeButton;
@end

@implementation LTChooseBaseViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self show];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.backgroundView];
    [self.backgroundView addSubview:self.contentView];
    [self.contentView addSubview:self.topView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.closeButton];
}

- (void)tapGestureEvent:(UITapGestureRecognizer *)gesture {
    CGPoint point = [gesture locationInView:self.contentView];
    CGPoint point2 = [gesture locationInView:self.closeButton];
    if (gesture.view == self.backgroundView && (![self.contentView.layer containsPoint:point] || [self.closeButton.layer containsPoint:point2]) ) {
        [self dismiss];
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }
    return YES;
}


- (void)show {
    self.backgroundView.backgroundColor = rgba(0, 0, 0, 0.0);
    [UIView animateWithDuration:0.25 animations:^{
        if (!self.backgourndColor) {
            self.backgroundView.backgroundColor = rgba(0, 0, 0, 0.6);
        }
        self.contentView.top = kScreenHeight - self.contentView.height;
    }];
}

- (void)dismiss {
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.top = kScreenHeight;
        self.backgroundView.backgroundColor = rgba(0, 0, 0, 0.0);
    } completion:^(BOOL finished) {
        if (self.hideBlock) {
            self.hideBlock();
        }
        [self dismissViewControllerAnimated:NO completion:NULL];
    }];
}

- (CGFloat)safeBottom {
    return [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, self.view.width, [self safeBottom] + 244)];
        _contentView.backgroundColor = [UIColor whiteColor];
        UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerTopRight;
        CGFloat cornerRadius = 8;
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_contentView.bounds
                                                       byRoundingCorners:corners
                                                             cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = _contentView.bounds;
        maskLayer.path = maskPath.CGPath;
        _contentView.layer.mask = maskLayer;
    }
    return _contentView;
}

- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureEvent:)];
        tap.delegate = self;
        [_backgroundView addGestureRecognizer:tap];
    }
    return _backgroundView;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _closeButton.frame = CGRectMake(self.view.width - 58, 0, 58, 50);
        [_closeButton setTitle:@"关闭" forState:UIControlStateNormal];
        _closeButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_closeButton setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
        [_closeButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

- (UIView *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.frame = CGRectMake(16, 0, self.view.width - 100, 50);
        _titleLabel.textColor = rgba(51, 51, 51, 1);
        _titleLabel.font = [UIFont systemFontOfSize:15];
    }
    return _titleLabel;
}

- (UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 49)];
        _topView.backgroundColor = rgba(248, 248, 248, 1);
    }
    return _topView;
}

@end
